# -*- coding: utf-8 -*-
# Create your views here.
'''http接口实现'''
from __future__ import unicode_literals

import json
import threading

from django.http import HttpResponse, StreamingHttpResponse
from django.shortcuts import render
from AndroidTest.service import monkey_ctrl,start_ctrl

mutex_monkeyTest = threading.Lock()

def index(request):
    '''根目录'''
    return render(request, 'index.html')

def frame_monekey(request):
    '''根目录'''
    return render(request, 'AndroidTest-v2.html')

def defaultFrame(request):
    '''根目录'''
    return render(request, 'index2.html')

def otaDetailFrame(request):
    '''OTA测试详解'''
    return render(request, 'otadetail.html')

def otaOperationFrame(request):
    '''OTA测试操作'''
    return render(request, 'upgradeSql.html')

def monkeyDetailFrame(request):
    '''monkey测试详解'''
    return render(request, 'monkeydetail.html')

def startOperationFrame(request):
    '''启动时间操作'''
    return render(request, 'starttime.html')

def startTimeDetail(request):
    '''启动时间详解'''
    return render(request, 'startdetail.html')


#monkey测试接口
def monkeyTest(request):
    json_data = json.loads(request.body)
    mutex_monkeyTest.acquire()
    seed = json_data['seed']
    apptype = json_data['apptype']
    # 开始执行monkey
    monkeyadb = monkey_ctrl.MonkeyServices()
    monkeyadb.monkey_adb(seed,apptype)
    # monkey执行结束
    json_resp = {"error_code": 0,
                 "msg": "monkey finished",
                 "data": {}}
    mutex_monkeyTest.release()
    return HttpResponse(json.dumps(json_resp))

# 下载monkey测试log 文件
def file_download(request):
    def file_iterator(file_name, chunk_size=512):
        with open(file_name) as f:
            while True:
                c = f.read(chunk_size)
                if c:
                    yield c
                else:
                    break
    the_file_name = "monkeyLog.txt"
    response = StreamingHttpResponse(file_iterator(the_file_name))
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = 'attachment;filename="{0}"'.format(the_file_name)
    return response

def readMonkey(request):
    mutex_monkeyTest.acquire()
    # 开始读取monkey
    monkeyadb = monkey_ctrl.MonkeyServices()
    monkeyVal=monkeyadb.readMonkeyLog()
    json_resp = {"error_code": 0,
                 "msg": "read finished",
                 "model": monkeyVal}
    mutex_monkeyTest.release()
    return HttpResponse(json.dumps(json_resp))

def startTimeTest(request):
    json_data = json.loads(request.body)
    num = json_data['num']
    page = json_data['page']
    version = json_data['version']
    # 开始执行startAdb
    controller=start_ctrl.Controller(int(num),page,version)
    controller.run()
    controller.saveDataToCsv()
    json_resp = {"error_code": 0,
                 "msg": "start finished",
                 "data": {}}
    return HttpResponse(json.dumps(json_resp))

def startTimeResult(request):
    # 开始读取csv文件
    csv0=start_ctrl.readStartCsv()
    csv1=csv0.readcsv()
    json_resp = {"error_code": 0,
                 "msg": "startContent",
                 "model": csv1}
    return HttpResponse(json.dumps(json_resp))
